import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

//定义store
export const useTasklistStore = defineStore('tasklist', () => {
  //任务列表
  var tasklist = ref([])
  //新增任务
  var addtask = (payload) => {//payload是传来的任务名称
    if(payload){
      tasklist.value.push({ id: tasklist.value.length + 1, name: payload, status: false })
    }
   
  }
  //查找任务
  var changeStatus = (payload) => {
    var task = tasklist.value.find((item) => {
      return item.id == payload
    })
    if (task) {
      task.status = !task.status
    }
  }
  //删除任务
  var deleteTask = (payload) => {
    var index = tasklist.value.findIndex((item) => {
      return item.id == payload
    })
    if (index!=-1) {
      tasklist.value.splice(index,1)
    }
  }
  //统计未完成数据
  var unFinishedCount=()=>{
    return tasklist.value.filter((item)=>!item.status).length
  }
  return {
    tasklist,
    addtask,
    changeStatus,
    deleteTask,
    unFinishedCount
  }
})
